package com.share.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.share.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * created by caifan 2018/06/11
 */
@RestController
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "findByIdFallback")
    @GetMapping("/user/{id}")
    public User findById(@PathVariable("id")Long id) {
        return this.restTemplate.getForObject("http://microservice-provider-user:7010/rest/user/" + id, User.class);
    }

    public User findByIdFallback(Long id) {
        User user = new User();
        user.setId(-1L);
        user.setUsername("default");
        user.setEmail("system@163.com");
        return user;
    }
}
